package com.guli.teacher.controller;


import com.guli.common.result.Result;
import com.guli.teacher.entity.EduChapter;
import com.guli.teacher.entity.vo.OneChapter;
import com.guli.teacher.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author guli
 * @since 2020-05-29
 */
@RestController
@RequestMapping("/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 根据课程id获取章节和小节
     * ：：Tree
     *  1 创建建一个对象，作为章节Vo，里面包括一个二级集合
     *  2 再创建二级的Vo（Video）
     *  3 根据课程id查询章节的列表，然后遍历这个列表，根据每一个章节的Id查询二级列表（Video）
     */
    @GetMapping("{courseId}")
    public Result getChapterAndVideoById(@PathVariable String courseId){
      List<OneChapter> list = chapterService.getChapterAndVideoById(courseId);
        return Result.ok().data("list", list);
    }

    @PostMapping("save")
    public Result save(@RequestBody EduChapter chapter){
        boolean save = chapterService.saveChapter(chapter);
        if (save){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @GetMapping("get/{id}")
    public Result  getChapterById(@PathVariable String id){
        EduChapter chapter = chapterService.getById(id);
        return Result.ok().data("chapter", chapter);
    }

    @PutMapping("update")
    public Result updateById(@RequestBody EduChapter chapter){
        boolean b = chapterService.updateChapterById(chapter);
        if (b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable String id) {
        Boolean flag = chapterService.removeChapterById(id);
        if (flag){
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

